package com.huobao.shangjia_version.activitys;

import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys.Act_DingDanGuanli;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ExitUtil;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Act_Work extends HuobaoBaseActivity implements OnClickListener {

	@ViewInject(R.id.radiogroup)
	private RadioGroup radiogroup;
	@ViewInject(R.id.rb_shouye)
	private RadioButton rb_shouye;
	@ViewInject(R.id.rb_fenlei)
	private Button rb_fenlei;
	@ViewInject(R.id.rb_jhc)
	private Button rb_jhc;
	@ViewInject(R.id.rb_wode)
	private Button rb_wode;

	@ViewInject(R.id.rtly)
	private RelativeLayout layout;
	@ViewInject(R.id.imageview)
	private ImageView imageview;
	@ViewInject(R.id.tv_storename)
	private TextView tv_storename;
	@ViewInject(R.id.tv_tel)
	private TextView tv_tel;
	@ViewInject(R.id.tv_1)
	private TextView tv_1;
	@ViewInject(R.id.tv_2)
	private TextView tv_2;
	@ViewInject(R.id.tv_3)
	private TextView tv_3;
	// 资金管理
	@ViewInject(R.id.llyt_1)
	private View llyt1;
	@ViewInject(R.id.llyt_10)
	private View llyt10;
	// 统计
	@ViewInject(R.id.llyt_2)
	private View llyt2;
	// 分享
	@ViewInject(R.id.llyt_3)
	private View llyt3;
	// 今日订单
	@ViewInject(R.id.view_1)
	private View view1;
	// 我的订单
	@ViewInject(R.id.view_5)
	private View view5;
	// 待发货订单
	@ViewInject(R.id.view_3)
	private View view3;
	// 在售商品
	@ViewInject(R.id.view_4)
	private View view4;
	// 退换货
	@ViewInject(R.id.view_2)
	private View view2;
	// 足迹
	@ViewInject(R.id.llyt_30)
	private View llyt_30;

	@Override
	protected int getContentView() {
		return R.layout.layout_work;
	}

	@Override
	protected String getTitleText() {
		return getString(R.string.title_10);
	}


	@Override
	protected void init() {
		initTopHight();
		llyt10.setOnClickListener(this);
		llyt1.setOnClickListener(this);
		llyt2.setOnClickListener(this);
		llyt3.setOnClickListener(this);
		view1.setOnClickListener(this);
		view2.setOnClickListener(this);
		view3.setOnClickListener(this);
		view4.setOnClickListener(this);
		llyt_30.setOnClickListener(this);
		view5.setOnClickListener(this);
		getData();
	}

	private void initTopHight() {
		Drawable drawable = getResources().getDrawable(R.mipmap.headbg);
		LayoutParams params = (LayoutParams) layout.getLayoutParams();
		params.width = ScreenUtil.getInstant().getDisWidth(context);
		params.height = ScreenUtil.getInstant().getDisWidth(context) * drawable.getIntrinsicHeight()
				/ drawable.getIntrinsicWidth();
		layout.setLayoutParams(params);
	}

	private String epay_dj;
	private String epay_money;
	String store_logo;
	String store_id;

	private void getData() {
		Map<String, String> map = HttpUtil.getParams("app", "shopmember");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String user_name = jsonObject.getString("user_name");
				String khzj = jsonObject.getString("khzj");
				String collect_mod = jsonObject.getString("collect_mod");
				JSONArray epay_money_array = jsonObject.getJSONArray("epay_money");
				epay_money = epay_money_array.getString(0);
				JSONArray epay_dj_array = jsonObject.getJSONArray("epay_dj");
				epay_dj = epay_dj_array.getString(0);
				JSONObject store_data_ob = jsonObject.getJSONObject("store_data");
				store_logo = store_data_ob.getString("store_logo");
				String store_name = store_data_ob.getString("store_name");
				store_id = store_data_ob.getString("store_id");
				ImageUtils.loadimg(imageview, store_logo);
				tv_storename.setText(store_name);
				tv_tel.setText(user_name);
				tv_1.setText(collect_mod);
				tv_2.setText("￥" + epay_money);
				tv_3.setText(khzj);
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	public void onStoreClick(View view) {
		Intent intent = new Intent(this, Act_MyStore.class);
		intent.putExtra("img", store_logo);
		intent.putExtra("id", store_id);
		intent.putExtra("epay_dj", epay_dj);
		intent.putExtra("epay_money", epay_money);
		startActivity(intent);
	}

	public void onJiaoyiClick(View view) {
		Intent intent = new Intent(this, Act_Store_Dingdan_Manager.class);
		startActivity(intent);
	}

	public void onProgectClick(View view) {
		Intent intent = new Intent(this, Act_Store_Progect_Manager.class);
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
			case R.id.llyt_1:
			case R.id.llyt_10:
				intent.setClass(context, Act_Zijin_Manager.class);
				intent.putExtra("epay_dj", epay_dj);
				intent.putExtra("epay_money", epay_money);
				startActivity(intent);
				break;
			case R.id.llyt_2:
				break;
			case R.id.llyt_3:
				intent.setClass(context, Act_Store_share.class);
				startActivity(intent);
				break;
			case R.id.view_1:
				intent.setClass(context, Act_Store_Dingdan_Manager.class);
				startActivity(intent);
				break;
			case R.id.view_2:
				intent.setClass(context, Act_Store_Dingdan_Manager.class);
				intent.putExtra("pos", 2);
				startActivity(intent);
				break;
			case R.id.view_3:
				intent.setClass(context, Act_Store_Progect_Manager.class);
				startActivity(intent);
				break;
			case R.id.view_4:
				intent.setClass(context, Act_Store_Dingdan_Manager.class);
				intent.putExtra("pos", 4);
				startActivity(intent);
				break;
			case R.id.view_5:
				intent.setClass(context, Act_DingDanGuanli.class);
				startActivity(intent);
				break;
			case R.id.llyt_30:
				// intent.setClass(context, Act_SeeHistory.class);
				// intent.putExtra("type", "zuji");
				// startActivity(intent);
				break;
		}
	}

	private long tempTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (tempTime == 0) {
				tempTime = System.currentTimeMillis();
				Toast.makeText(context, "再按一次退出程序!", Toast.LENGTH_SHORT).show();
				return false;
			}
			if ((System.currentTimeMillis() - tempTime) <= 2 * 1000) {
				ExitUtil.getInstant().exitApp(context);
				finish();
				return false;
			}
			Toast.makeText(context, "再按一次退出程序!", Toast.LENGTH_SHORT).show();
			tempTime = System.currentTimeMillis();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
